//
//  PlayerViewController.m
//  LOLVideoPlayer
//
//  Created by SXW on 16/3/28.
//  Copyright © 2016年 盛宣伟. All rights reserved.
//

#import "PlayerViewController.h"
@interface PlayerViewController ()

//@property (nonatomic, strong) MPMoviePlayerController *MVPlayer;
@property (nonatomic, strong) MPMoviePlayerViewController *MVPlayer;
@end

@implementation PlayerViewController

-(void)viewWillAppear:(BOOL)animated{

    self.navigationController.navigationBar.hidden = NO;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.vedioToUrl = [NSString stringWithFormat:@"http://www.demaxiya.com/app/index.php?m=play&vid=%@&quality=1&rand_1=1",self.ids];
    
//    NSURL *url = [NSURL URLWithString:self.vedioToUrl];
//    NSLog(@"%@",url);
//    
//    //初始化属性的时候，尽量用下划线调用属性，不然涉及到内存问题
//    _MVPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
//    _MVPlayer.view.frame = self.view.frame;
//    [self.view addSubview:_MVPlayer.view];
//    //播放视频
//    [_MVPlayer play];
    
    [self playVideo:_vedioToUrl];
    
    

}

#pragma mark - 播放视频

- (void)playVideo:(NSString *)playUrl{
    self.MVPlayer = [[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL URLWithString:self.vedioToUrl]];
    self.MVPlayer.moviePlayer.shouldAutoplay = YES;
    
    [self.MVPlayer.moviePlayer prepareToPlay];
    [self.MVPlayer.moviePlayer play];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(playEnd:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    [self presentViewController:self.MVPlayer animated:YES completion:nil];
}

-(void)playEnd:(NSNotification *)notify{
    NSInteger reason = [[[notify userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
    switch (reason) {
        case 1:
            NSLog(@"正常结束");
            break;
        case 2:
            NSLog(@"用户退出");
            [self.navigationController popViewControllerAnimated:NO];
            break;
        default:
            NSLog(@"异常退出");
            break;
    }
    
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



















@end
